import request from '@/utils/request'

// 模拟数据
let mockNotices = [
  {
    id: 1,
    title: '关于2024年春节放假安排的通知',
    content: '根据国家法定节假日安排，结合公司实际情况，现将2024年春节放假安排通知如下...',
    type: 1,
    typeName: '通知',
    status: 1,
    statusName: '已发布',
    publisherName: '管理员',
    publishTime: '2024-01-15 10:00:00',
    startTime: '2024-01-15 00:00:00',
    endTime: '2024-02-15 23:59:59',
    viewCount: 156,
    createTime: '2024-01-15 09:30:00',
    updateTime: '2024-01-15 10:00:00'
  },
  {
    id: 2,
    title: '系统维护公告',
    content: '为了提升系统性能，将于本周六进行系统维护，维护期间系统将无法访问...',
    type: 2,
    typeName: '公告',
    status: 1,
    statusName: '已发布',
    publisherName: '技术部',
    publishTime: '2024-01-10 14:30:00',
    startTime: '2024-01-10 00:00:00',
    endTime: '2024-01-20 23:59:59',
    viewCount: 89,
    createTime: '2024-01-10 14:00:00',
    updateTime: '2024-01-10 14:30:00'
  },
  {
    id: 3,
    title: '年度考核提醒',
    content: '请各部门于本月底前完成年度考核工作，相关材料请按时提交...',
    type: 3,
    typeName: '提醒',
    status: 2,
    statusName: '草稿',
    publisherName: '人事部',
    publishTime: null,
    startTime: '2024-01-25 00:00:00',
    endTime: '2024-01-31 23:59:59',
    viewCount: 0,
    createTime: '2024-01-08 16:20:00',
    updateTime: '2024-01-08 16:20:00'
  },
  {
    id: 4,
    title: '新员工培训通知',
    content: '本周五将举行新员工入职培训，请相关同事准时参加...',
    type: 1,
    typeName: '通知',
    status: 3,
    statusName: '已撤回',
    publisherName: '培训部',
    publishTime: '2024-01-05 09:00:00',
    startTime: '2024-01-05 00:00:00',
    endTime: '2024-01-12 23:59:59',
    viewCount: 45,
    createTime: '2024-01-04 15:30:00',
    updateTime: '2024-01-06 11:00:00'
  }
]

let idCounter = 5

/**
 * 公告管理API（模拟数据）
 */
export const noticeApi = {
  /**
   * 分页查询公告列表
   */
  getNoticeList(params) {
    return new Promise((resolve) => {
      setTimeout(() => {
        let { title, type, status, page = 1, size = 10 } = params
        
        // 过滤数据
        let filteredData = mockNotices.filter(item => {
          let match = true
          if (title && !item.title.includes(title)) {
            match = false
          }
          if (type && item.type !== type) {
            match = false
          }
          if (status && item.status !== status) {
            match = false
          }
          return match
        })
        
        // 分页
        const start = (page - 1) * size
        const end = start + size
        const paginatedData = filteredData.slice(start, end)
        
        resolve({
          code: 1,
          data: {
            rows: paginatedData,
            total: filteredData.length
          },
          msg: '获取公告列表成功'
        })
      }, 500)
    })
  },

  /**
   * 根据ID查询公告
   */
  getNoticeById(id) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const notice = mockNotices.find(item => item.id === id)
        if (notice) {
          resolve({
            code: 1,
            data: notice,
            msg: '获取公告详情成功'
          })
        } else {
          resolve({
            code: 0,
            msg: '公告不存在'
          })
        }
      }, 300)
    })
  },

  /**
   * 新增公告
   */
  addNotice(data) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newNotice = {
          ...data,
          id: idCounter++,
          viewCount: 0,
          createTime: new Date().toLocaleString('zh-CN'),
          updateTime: new Date().toLocaleString('zh-CN')
        }
        mockNotices.unshift(newNotice)
        
        resolve({
          code: 1,
          msg: '新增公告成功'
        })
      }, 500)
    })
  },

  /**
   * 更新公告
   */
  updateNotice(data) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = mockNotices.findIndex(item => item.id === data.id)
        if (index !== -1) {
          mockNotices[index] = {
            ...mockNotices[index],
            ...data,
            updateTime: new Date().toLocaleString('zh-CN')
          }
          resolve({
            code: 1,
            msg: '更新公告成功'
          })
        } else {
          resolve({
            code: 0,
            msg: '公告不存在'
          })
        }
      }, 500)
    })
  },

  /**
   * 删除公告
   */
  deleteNotice(id) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = mockNotices.findIndex(item => item.id === id)
        if (index !== -1) {
          mockNotices.splice(index, 1)
          resolve({
            code: 1,
            msg: '删除公告成功'
          })
        } else {
          resolve({
            code: 0,
            msg: '公告不存在'
          })
        }
      }, 500)
    })
  },

  /**
   * 更新公告状态
   */
  updateNoticeStatus(id, status) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = mockNotices.findIndex(item => item.id === id)
        if (index !== -1) {
          const statusMap = { 1: '已发布', 2: '草稿', 3: '已撤回' }
          mockNotices[index].status = status
          mockNotices[index].statusName = statusMap[status]
          if (status === 1) {
            mockNotices[index].publishTime = new Date().toLocaleString('zh-CN')
          }
          mockNotices[index].updateTime = new Date().toLocaleString('zh-CN')
          
          resolve({
            code: 1,
            msg: '更新公告状态成功'
          })
        } else {
          resolve({
            code: 0,
            msg: '公告不存在'
          })
        }
      }, 500)
    })
  }
}